﻿using Kingdee.BOS.WebApi.Client;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*
{
	"Model": {
		"FID": "0",
		"FBillTypeID": {
			"FNUMBER": "XSDD01_SYS"
		},
		"FDate": "2019-08-01",
		"FSaleOrgId": {
			"FNumber": "WL01"C:\Users\liwei\Src\AgencyManagerSystem\ErpBridge\Program.cs
		},
		"FCustId": {
			"FNumber": "CUST0004"
		},
		"FSalerId": {
			"FNumber": "FJ00013"
		},
		"FSaleOrderFinance": {
			"FSettleCurrId": {
				"FNumber": "PRE001"
			},
			"FEXCHANGERATE": "1"
		},
		"FSaleOrderEntry": [{
			"FMaterialId": {
				"FNumber": "fgg0002003"
			},
			"FUnitID": {
				"FNumber": "Pcs"
			},
			"FQty": "1",
			"FPrice": "20",
			"FDeliveryDate": "2019-08-01",
			"FSettleOrgIds": {
				"FNumber": "FS01"
			},
			"FOUTLMTUNIT": "销售单位"
		}]
	}
}
*/

namespace ErpBridge
{
    class Program
    {
        static void Main(string[] args)
        {

            string arg = args[0];
            File.WriteAllText("c:\\dbg.log", args[0]);

            string json = arg;
			try
			{
				if (arg.StartsWith("-file"))
				{
					json = File.ReadAllText("bdg\\" + arg.Substring(5, arg.Length - 5));
					File.Delete("bdg\\" + arg.Substring(5, arg.Length - 5));
				}
			}
			catch (Exception ex)
			{
				File.AppendAllText("bdg\\info.log", ex.Message);
			}
            try
            {
                //string json = "";
                K3CloudApiClient client = new K3CloudApiClient("http://127.0.0.1/K3Cloud/");
                //var ret = client.ValidateLogin("5ce72b40a4cae0", "李伟权", "999abc", 2052);
                var ret = client.ValidateLogin("5ebd63e642b0b4", "李伟权", "999abc", 2052);
                var result = JObject.Parse(ret)["LoginResultType"].Value<int>();
				// 登陆成功

				if (result == 1)
				{
					//var res = client.Save("SAL_SaleOrder", "{\"Model\": {\"FID\": \"0\",\"FBillTypeID\": {\"FNUMBER\": \"XSDD01_SYS\"},\"FDate\": \"2019-08-01\",\"FSaleOrgId\": {\"FNumber\": \"WL01\"},\"FCustId\": {\"FNumber\": \"CUST0004\"},\"FSalerId\": {\"FNumber\": \"FJ00013\"},\"FSaleOrderFinance\": {\"FSettleCurrId\": {\"FNumber\": \"PRE001\"},\"FEXCHANGERATE\":\"1\"},\"FSaleOrderEntry\": [{\"FMaterialId\": {\"FNumber\": \"fgg0002003\"},\"FUnitID\": {\"FNumber\": \"Pcs\"},\"FQty\": \"1\",\"FPrice\": \"20\",\"FDeliveryDate\": \"2019-08-01\",\"FSettleOrgIds\": {\"FNumber\": \"FS01\"},\"FOUTLMTUNIT\": \"销售单位\"}]}}");
					var res = client.Save("SAL_SaleOrder", json);
					if (JObject.Parse(res)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() == "TRUE")
					{
						Console.WriteLine(0 + "##" + res.ToString());
					}
					else
					{
						Console.WriteLine(1 + "##" + res.ToString() + "/" + json);
					}
				}
				else
				{
                    File.AppendAllText("bdg\\info.log", ret);
                    Console.WriteLine(1 + "##系统连接失败");
				}

            }
            catch(Exception ex)
            {
				File.AppendAllText("bdg\\info.log", ex.Message);
				Console.WriteLine(2+"##"+ex.Message+ "/"+json);
            }
        }
    }
}
